package edu.sdcet.m10d1302.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.sdcet.m10d1302.Entity.Student;
import edu.sdcet.m10d1302.Mapper.StudentMapper;
import edu.sdcet.m10d1302.Service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 获取所有学生信息
     * @return
     */
    @Override
    public List<Student> getAllStudents() {
        List<Student> students = studentMapper.selectList(null);
        log.info("获取所有学生信息，共" + students.size() + "条数据");
        return students;
    }

    /**
     * 根据学号查询单个学生信息
     * @param studentId
     * @return
     */
    @Override
    public Student getStudentById(String studentId) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);
        Student student = studentMapper.selectOne(queryWrapper);
        log.info("根据学号查询学生信息，学号：" + studentId + "，结果：" + (student != null ? "找到" : "未找到"));
        return student;
    }

    /**
     * 根据学号修改学生信息
     * @param student
     * @return
     */
    @Override
    public void updateStudent(Student student) {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        String studentId = student.getStudentId();
        student.setStudentId(studentId);
        student.setUpdateTime(time);
        

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);
        

        int result = studentMapper.update(student, queryWrapper);
          if (result != 1) {
              log.info("更新学生信息，学号：" + studentId );
          }else {
              log.info("失败" + studentId );
          }

    }

    /**
     * 新增学生
     * @param student
     * @return
     */
    @Override
    public void addStudent(Student student) {
        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        student.setCreateTime(time);
        student.setUpdateTime(time);

        int result = studentMapper.insert(student);
        if (result == 1) {
            log.info("新增学生信息，学号：" + student.getStudentId());
        }else{
            log.info("新增学生信息失败" + student.getStudentId());
        }


    }

    /**
     * 删除学生
     * @param studentId
     * @return
     */
    @Override
    public void deleteStudent(String studentId) {

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_id", studentId);

        int result = studentMapper.delete(queryWrapper);
        if (result > 0) {
            log.info("删除学生信息，学号：" + studentId);
        }else {
            log.info("失败" + studentId);
        }


    }

}
